package com.zhangdi.dwcache.redis.dao.impl;

import com.zhangdi.dwcache.redis.dao.RCacheBaseDao;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;

import java.util.concurrent.TimeUnit;

public class RCacheStringTemplate<K, V> implements RCacheBaseDao<K, V> {
    private RedisTemplate<K, V> template;
    @Autowired
    public void setTemplate(RedisTemplate<K, V> mRedisTemplate) {
        this.template = mRedisTemplate;
    }

    @Override
    public boolean save(K key, V data) {
        try {
            template.opsForValue().set(key, data);
            return true;
        } catch (Exception e) {
            return false;
        }
    }

    @Override
    public boolean saveByExpire(K key, V data, long time, TimeUnit unit) {
        try {
            template.opsForValue().set(key, data, time, unit);
            return true;
        } catch (Exception e) {
            e.printStackTrace();
            return false;
        }
    }

    @Override
    public boolean remove(K key) {
        Boolean b = template.delete(key);
        if(b == null){
            return false;
        }
        return b;
    }

    @Override
    public V get(K key) {
        return template.opsForValue().get(key);
    }
}